﻿Public Class TunnelWall
    Private IPAddr As String
    Private Port As Integer
    Private BandWidthLimit As Integer

    Private PIDofCMD As Integer

    Private SecretKey As String = ""


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            Dim cmdToKill As Process
            cmdToKill = Process.GetProcessById(PIDofCMD)
            cmdToKill.Kill()

            Dim polipoToKill() As Process
            polipoToKill = Process.GetProcessesByName("polipo")
            For i = 0 To UBound(polipoToKill)
                polipoToKill(i).Kill()
            Next
        Catch ex As Exception
        End Try
        Dim RunPolipo As Threading.Thread = New Threading.Thread(AddressOf Poliop)
        RunPolipo.Start()
        Dim RunTunnel As Threading.Thread = New Threading.Thread(AddressOf TunnelStart)
        RunTunnel.Start()
        Try
            IPAddr = TextIP.Text
            Port = TextPort.Text
            If Port < 1 Or Port > 65536 Then
                GoTo catcherror
            End If


            BandWidthLimit = TextWidth.Text
catcherror: Catch
            MsgBox("Illegal character!", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly)
            End
        End Try
    End Sub



    Public Sub Poliop()
        Try
            Dim Polipo As Process = New Process
            Polipo.StartInfo.FileName = "cmd.exe"
            Polipo.StartInfo.CreateNoWindow = True
            Polipo.StartInfo.UseShellExecute = False
            Polipo.StartInfo.RedirectStandardInput = True
            Polipo.StartInfo.RedirectStandardOutput = True
            Polipo.Start()
            PIDofCMD = Polipo.Id
            Dim Output As IO.StreamReader = Polipo.StandardOutput
            Dim Input As IO.StreamWriter = Polipo.StandardInput
            Input.WriteLine("polipo -c config.txt 2>polipoerr.log >polipo.log ")
            Polipo.WaitForExit()
        Catch ex As Exception
            MsgBox("Failed to open polipo.exe!", MsgBoxStyle.OkOnly)
        End Try
        
    End Sub
    Public Sub TunnelStart()
        Try
            Threading.Thread.Sleep(1000)
            Dim Tunnel As Process = New Process()
            Dim Output As System.IO.StreamReader
            Dim StdError As System.IO.StreamReader

            Dim Arguments As String = " -s -l " & Port & " -i " & IPAddr & " -p 51949 -w " & BandWidthLimit
            Tunnel.StartInfo.FileName = "encryptedtunnel.exe"
            Tunnel.StartInfo.Arguments = Arguments
            Tunnel.StartInfo.CreateNoWindow = True
            Tunnel.StartInfo.RedirectStandardOutput = True
            Tunnel.StartInfo.RedirectStandardError = True
            Tunnel.StartInfo.UseShellExecute = False
            Tunnel.Start()
            StdError = Tunnel.StandardError



            Output = Tunnel.StandardOutput
            If Output.ReadLine() = Nothing Then

                Dim ErrorText As String = StdError.ReadLine
                If ErrorText <> Nothing Then
                    MsgBox("Encryptedtunnel Error!" & vbCrLf & ErrorText, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly)
                    Exit Try
                End If

            End If

            SecretKey = Output.ReadLine

            MsgBox("Received secret key!" & SecretKey, MsgBoxStyle.Information + MsgBoxStyle.OkOnly)
            Label1.Text = "Key"
            TextPort.Text = Microsoft.VisualBasic.Right(SecretKey, 64)

        Catch ex As Exception
            MsgBox("TunnelError" & vbCrLf & ex.Message)
        End Try

    End Sub

    Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
        Try
            Dim cmdToKill As Process
            cmdToKill = Process.GetProcessById(PIDofCMD)
            cmdToKill.Kill()

            Dim polipoToKill() As Process
            polipoToKill = Process.GetProcessesByName("polipo")
            For i = 0 To UBound(polipoToKill)
                polipoToKill(i).Kill()
            Next


            Dim tunnelToKill() As Process
            tunnelToKill = Process.GetProcessesByName("encryptedtunnel")
            For i = 0 To UBound(tunnelToKill)
                tunnelToKill(i).Kill()
            Next

        Catch ex As Exception
            MsgBox("Can not term the process automatically,Maybe you need to kill them manually", MsgBoxStyle.Information + MsgBoxStyle.OkOnly)

        End Try


        Me.Close()
        End
    End Sub

    Private Sub Show_Hide_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Show_Hide.Click
        If Me.WindowState = FormWindowState.Minimized Then
            Me.Show()
            Me.WindowState = FormWindowState.Normal
            Me.ShowInTaskbar = True

        Else
            Me.WindowState = FormWindowState.Minimized
            Me.Hide()
            Me.ShowInTaskbar = False
        End If
    End Sub

    Private Sub Button2_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Try
            Dim cmdToKill As Process
            cmdToKill = Process.GetProcessById(PIDofCMD)
            cmdToKill.Kill()

            Dim polipoToKill() As Process
            polipoToKill = Process.GetProcessesByName("polipo")
            For i = 0 To UBound(polipoToKill)
                polipoToKill(i).Kill()
            Next

            Dim tunnelToKill() As Process
            tunnelToKill = Process.GetProcessesByName("encryptedtunnel")
            For i = 0 To UBound(tunnelToKill)
                tunnelToKill(i).Kill()
            Next
        Catch ex As Exception
            MsgBox("Can not term the process automatically,Maybe you need to kill them manually", MsgBoxStyle.Information + MsgBoxStyle.OkOnly)

        End Try


        Me.Close()
        End


    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Me.WindowState = FormWindowState.Minimized
        Me.Hide()
        Me.ShowInTaskbar = False
    End Sub

    Private Sub NotifyIcon1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles NotifyIcon1.DoubleClick
        If Me.WindowState = FormWindowState.Minimized Then
            Me.WindowState = FormWindowState.Normal
            Me.ShowInTaskbar = True
            Me.Show()
        Else
            Me.WindowState = FormWindowState.Minimized
            Me.Hide()
            Me.ShowInTaskbar = False
        End If
    End Sub

    Private Sub TunnelWall_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Control.CheckForIllegalCrossThreadCalls = False
    End Sub
End Class
